using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZedGraph;

namespace FTFSmartCar
{
    public partial class BatteryViewerForm : Form
    {
        
        #region visualization variables
        PointPairList VoltagePointPairList = new PointPairList();
        GraphPane  MyPane = new GraphPane();
       
        
        public const int SampleRate = 10;
        public const int TimeHistoryToKeep = 5;
        public const int VoltageHistoryLength = TimeHistoryToKeep * SampleRate;

        double[] VoltageHistory = new double[VoltageHistoryLength];
        int VoltageHistoryIndex;

        #endregion

        public BatteryViewerForm()
        {
            InitializeComponent();
            InitWaveViewer();
            ResizeGraph();
        }

        
        public void PostNextVoltageReading(double VoltageIn)
        {
            VoltageHistory[VoltageHistoryIndex] = VoltageIn;
            VoltageHistoryIndex++;
            if (VoltageHistoryIndex >= VoltageHistoryLength)
                VoltageHistoryIndex= 0;
        }
        

        void InitWaveViewer()
        {
              
                MyPane.Title.Text = "Battery Voltage ";
                MyPane.XAxis.Title.Text = "Time (Seconds)";
                MyPane.YAxis.Title.Text = "Voltage (volts)";
                MyPane.YAxis.Scale.MaxAuto = false;
                MyPane.YAxis.Scale.MinAuto = false;
                MyPane.YAxis.Scale.Min = 0;
                MyPane.YAxis.Scale.Max = 10;
                MyPane.YAxis.MajorGrid.IsVisible = true;

                MyPane.XAxis.Scale.MaxAuto = false;
                MyPane.XAxis.Scale.MinAuto = false;
                MyPane.XAxis.Scale.Min = -TimeHistoryToKeep;
                MyPane.XAxis.Scale.MinAuto = false;
                MyPane.XAxis.Scale.Max = 0;

                MyPane.XAxis.Scale.MaxGrace = 0.2;
                MyPane.XAxis.Scale.MinGrace = 0.2;

                MyPane.XAxis.MajorGrid.IsVisible = true;

                BatteryGraph.MasterPane.PaneList.Clear();
                BatteryGraph.MasterPane.PaneList.Add(MyPane);
                

            
            BatteryGraph.AxisChange();

          
            for(int i=0;i<VoltageHistoryLength;i++)
            {
                VoltageHistory[i] = 0;
            }

        }

        private void UpdateGraph()
        {
            double t = 0;

           VoltagePointPairList.Clear();

           int CurrentIndex = VoltageHistoryIndex;

           for (int i = 0; i < VoltageHistoryLength; i++)
                {


                    t = (((double)i / (double)VoltageHistoryLength) * (double)TimeHistoryToKeep) - (double)TimeHistoryToKeep;

                        VoltagePointPairList.Add(t, VoltageHistory[CurrentIndex]);

                        CurrentIndex++;

                        if (CurrentIndex >= VoltageHistoryLength)
                            CurrentIndex = 0;
                 }


           MyPane.CurveList.Clear();

            //LineItem[] MyCurve = new LineItem[6];

           MyPane.AddCurve("Battery Voltage", VoltagePointPairList, Color.Red, SymbolType.Circle);
           BatteryGraph.AxisChange();
           BatteryGraph.Invalidate();
         }

        #region GUI Callbacks

        private void WaveViewer_Resize(object sender, EventArgs e)
        {
            ResizeGraph();
        }

        void ResizeGraph()
        {
            BatteryGraph.Location = new Point(10, 10);
            // Leave a small margin around the outside of the control
            BatteryGraph.Size = new Size(ClientRectangle.Width - 20,
                                    ClientRectangle.Height - 20);

            UpdateGraph();
        }
        private void WaveViewer_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
             this.Hide();
        }
        #endregion

        private void WindowUpdateTimer_Tick(object sender, EventArgs e)
        {
            UpdateGraph();
            this.Invalidate();
        }


    }
}